/*------------------------------------------------------------------------
* Seedset-generator
* $Id$
* Copyright (c) 2008 Regents of the University of Minnesota.
* All Rights Reserved by the Regents of the University of Minnesota.
* This file contains Original Code and/or Modifications of Original
* Code as defined in the MAGNET Public Source License, which is
* available at http://www.cs.umn.edu/magnet/source_license_v1.txt.
* You may not use this file except in compliance with the License.
*------------------------------------------------------------------------
*/

//
//  TreeNodeRenderer.java
//  
//

//From thomas kloeber - java developer forums
//http://forum.java.sun.com/thread.jspa?forumID=57&threadID=596554

package edu.umn.cs.tac.seedsetgen;

import java.awt.Component;
import java.awt.Color;

import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultMutableTreeNode;

import javax.swing.ImageIcon;
import javax.swing.Icon; 

public class TreeNodeRenderer extends DefaultTreeCellRenderer
{
    Color randomColor = (new Color(0, 150, 0));
    Color controlledColor = (new Color(150, 0, 0));
    Color partialColor = (new Color(150, 150, 0));
    ImageIcon randomFolderIcon = createImageIcon("/images/seedsetgen/green-ball.gif");
    ImageIcon controlledFolderIcon = createImageIcon("/images/seedsetgen/red-ball.gif");
    ImageIcon partialFolderIcon = createImageIcon("/images/seedsetgen/yellow-ball.gif");
    ImageIcon randomIcon = createImageIcon("/images/seedsetgen/green-ball.gif");
    ImageIcon controlledIcon = createImageIcon("/images/seedsetgen/red-ball.gif");
    ImageIcon partialIcon = createImageIcon("/images/seedsetgen/yellow-ball.gif");


    public TreeNodeRenderer()
    {
        setOpaque(true);
    }

    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus)
    {
        super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
        Color bg;
        ImageIcon im;

        try
        {
            if (((SeedParameter)(((DefaultMutableTreeNode)value).getUserObject())).getControlType() == 0)
            {
                bg = randomColor;
                im = randomIcon;
            }
            else if (((SeedParameter)(((DefaultMutableTreeNode)value).getUserObject())).getControlType() == 1)
            {
                bg = controlledColor;
                im = controlledIcon;
            }
            else
            {
                bg = partialColor;
                im = partialIcon;
            }
        }
        catch (Exception e)
        { 
            bg = tree.getBackground(); 
            im = (ImageIcon)((DefaultTreeCellRenderer)(tree.getCellRenderer())).getLeafIcon();
        }

        setBackground(Color.white);
        setForeground(selected ? bg : Color.black);

        setIcon(im);

        return this;
    }

    /** Returns an ImageIcon, or null if the path was invalid. */
    protected static ImageIcon createImageIcon(String path)
    {
        java.net.URL imgURL = TreeNodeRenderer.class.getResource(path);
        if (imgURL != null)
        {
            return new ImageIcon(imgURL);
        }
        else
        {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }

}
